package com.taint.callgraph;

import java.util.Stack;

import static com.taint.callgraph.CallStack.PrintStack;

public class CallGraph {
    private Long threadId;

    Stack<TraceItem> stacks=new Stack<>();

    public CallGraph(long threadId) {
        this.threadId=threadId;
    }

    public void called(TraceItem stackItem) {
        //第一个栈
        if (!stacks.isEmpty()) {
            TraceItem top = stacks.peek();
        }
        stacks.push(stackItem);

    }

    public void exited(TraceItem item) {

        Stack<TraceItem> trace=new Stack<>();
        int removed = 0;
        boolean found = false;
        while (!stacks.isEmpty() && !found) {
            removed++;
            TraceItem topItem = stacks.pop();
            trace.push(topItem);
            if (topItem.equals(item)) {
                found = true;
                //System.out.println("I found Stack!!!");
                //https://stackoverflow.com/questions/45944793/how-to-print-java-method-call-stack
                StackTraceElement[] stackTraceElements= Thread.currentThread().getStackTrace();
                String sb=PrintStack(stackTraceElements);
                //Logger.Log(sb);
            }
        }
        if (removed != 1) {
            return;
        }
        if (!found) {
            return;
        }

    }
}